#include "include/gudb.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void print_result(result_t* result) {
    if (!result) {
        printf("No result\n");
        return;
    }
    
    // Print column headers
    for (uint32_t i = 0; i < result->col_count; i++) {
        printf("%-20s", result->column_names[i] ? result->column_names[i] : "NULL");
    }
    printf("\n");
    
    // Print separator
    for (uint32_t i = 0; i < result->col_count; i++) {
        printf("%-20s", "--------------------");
    }
    printf("\n");
    
    // Print rows
    for (uint32_t i = 0; i < result->row_count; i++) {
        for (uint32_t j = 0; j < result->col_count; j++) {
            printf("%-20s", result->rows[i] && result->rows[i][j] ? result->rows[i][j] : "NULL");
        }
        printf("\n");
    }
    printf("\n");
}

int main() {
    printf("=== DESCRIBE Command Test ===\n");
    
    // Create test database
    system("mkdir -p /tmp/test_describe_db");
    
    gudb* db = gudb_open("/tmp/test_describe_db", GU_OPEN_CREATE, "admin", "admin");
    if (!db) {
        printf("❌ Failed to create database\n");
        return 1;
    }
    printf("✅ Database created successfully\n");
    
    result_t* result = NULL;
    int ret;
    
    // Start transaction
    gudb_txn* txn = gudb_begin(db, 0);
    if (!txn) {
        printf("❌ Failed to begin transaction\n");
        gudb_close(db);
        return 1;
    }
    
    // Test 1: Create a test table
    printf("\n1. Creating test table...\n");
    ret = gudb_exec(txn, "CREATE TABLE test_products (id INT PRIMARY KEY, name VARCHAR(100) NOT NULL, price FLOAT DEFAULT 0.0);", NULL, &result);
    if (ret != GU_OK) {
        printf("❌ Failed to create table: %d\n", ret);
    } else {
        printf("✅ Table created successfully\n");
        if (result) gudb_free_result(result);
    }
    
    // Test 2: Test DESCRIBE command
    printf("\n2. Testing DESCRIBE command...\n");
    ret = gudb_exec(txn, "DESCRIBE test_products;", NULL, &result);
    if (ret != GU_OK) {
        printf("❌ DESCRIBE command failed: %d\n", ret);
    } else {
        printf("✅ DESCRIBE command succeeded\n");
        print_result(result);
        if (result) gudb_free_result(result);
    }
    
    // Test 3: Test DESC command
    printf("3. Testing DESC command...\n");
    ret = gudb_exec(txn, "DESC test_products;", NULL, &result);
    if (ret != GU_OK) {
        printf("❌ DESC command failed: %d\n", ret);
    } else {
        printf("✅ DESC command succeeded\n");
        print_result(result);
        if (result) gudb_free_result(result);
    }
    
    // Test 4: Test SHOW DATABASES
    printf("4. Testing SHOW DATABASES...\n");
    ret = gudb_exec(txn, "SHOW DATABASES;", NULL, &result);
    if (ret != GU_OK) {
        printf("❌ SHOW DATABASES failed: %d\n", ret);
    } else {
        printf("✅ SHOW DATABASES succeeded\n");
        print_result(result);
        if (result) gudb_free_result(result);
    }
    
    // Test 5: Test SHOW TABLES
    printf("5. Testing SHOW TABLES...\n");
    ret = gudb_exec(txn, "SHOW TABLES;", NULL, &result);
    if (ret != GU_OK) {
        printf("❌ SHOW TABLES failed: %d\n", ret);
    } else {
        printf("✅ SHOW TABLES succeeded\n");
        print_result(result);
        if (result) gudb_free_result(result);
    }
    
    // Test 6: Test SHOW COLUMNS FROM
    printf("6. Testing SHOW COLUMNS FROM...\n");
    ret = gudb_exec(txn, "SHOW COLUMNS FROM test_products;", NULL, &result);
    if (ret != GU_OK) {
        printf("❌ SHOW COLUMNS FROM failed: %d\n", ret);
    } else {
        printf("✅ SHOW COLUMNS FROM succeeded\n");
        print_result(result);
        if (result) gudb_free_result(result);
    }
    
    // Test 7: Test SHOW STATUS
    printf("7. Testing SHOW STATUS...\n");
    ret = gudb_exec(txn, "SHOW STATUS;", NULL, &result);
    if (ret != GU_OK) {
        printf("❌ SHOW STATUS failed: %d\n", ret);
    } else {
        printf("✅ SHOW STATUS succeeded\n");
        print_result(result);
        if (result) gudb_free_result(result);
    }
    
    // Commit transaction
    gudb_commit(txn);
    
    // Cleanup
    gudb_close(db);
    system("rm -rf /tmp/test_describe_db");
    
    printf("\n=== Test Complete ===\n");
    return 0;
}